package com.example.community.controller;

import com.example.community.entity.DiscussPost;
import com.example.community.entity.Page;
import com.example.community.entity.User;
import com.example.community.service.CommentService;
import com.example.community.service.DataService;
import com.example.community.service.DiscussPostService;
import com.example.community.service.UserService;
import com.example.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class DataController {

    @Autowired
    private DataService dataService;

    @Autowired
    private UserService userService;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private CommentService commentService;

    //统计页面
    @RequestMapping(path = "/data",method = {RequestMethod.GET,RequestMethod.POST})
    public String getDataPage(){
        return "/site/admin/data";
    }
    //统计网站UV
    @RequestMapping(path = "/data/uv",method = RequestMethod.POST)
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd")Date end, Model model){
        long uv = dataService.calculateUV(start,end);
        model.addAttribute("uvResult",uv);
        model.addAttribute("uvStartDate",start);
        model.addAttribute("uvEndDate",end);
//        return "/site/admin/data";
        return "forward:/data";
    }

    //统计活跃用户
    @RequestMapping(path = "/data/dau",method = RequestMethod.POST)
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd")Date end, Model model){
        long dau = dataService.calculateDAU(start,end);
        model.addAttribute("dauResult",dau);
        model.addAttribute("dauStartDate",start);
        model.addAttribute("dauEndDate",end);
//        return "/site/admin/data";
        return "forward:/data";
    }

    @RequestMapping(path = "/data/allUser",method = RequestMethod.GET)
    public String getAllUser( Model model, Page page){

        page.setRows(userService.totalUser());
        page.setPath("/data/allUser");
        page.setLimit(5);
        List<User> userList = userService.findAllUser(page.getOffset(),page.getLimit());
        model.addAttribute("userList",userList);

        return "/site/admin/user";
    }
    @RequestMapping(path = "/data/searchUser",method = RequestMethod.GET)
    public String getSomeUser(Model model, Page page,@RequestParam("keyword")String keyword){
        page.setPath("/data/searchUser?keyword="+keyword);
        page.setLimit(5);
        List<User> userList = userService.findSomeUser(page.getOffset(),page.getLimit(),keyword);
        page.setRows(userService.totalSomeUser(keyword));
        model.addAttribute("userList",userList);
        model.addAttribute("keyword",keyword);
        return "/site/admin/user";
    }

    @RequestMapping(path = "/data/black",method = RequestMethod.POST)
    @ResponseBody
    public String blackUser1(int id){

        userService.updateStatus(id,3);

        return CommunityUtil.getJSONString(3,"拉黑成功");

    }

    @RequestMapping(path = "/data/unblack",method = RequestMethod.POST)
    @ResponseBody
    public String unblackUser1(int id){

        userService.updateStatus(id,1);

        return CommunityUtil.getJSONString(1, "取消拉黑成功");

    }

    @RequestMapping(path = "/data/allBlackUser",method = RequestMethod.GET)
    public String getAllBlackUser(Model model, Page page){

        page.setPath("/data/allBlackUser");
        page.setLimit(5);
        List<User> userList = userService.findAllBlackUser(page.getOffset(),page.getLimit());
        model.addAttribute("userList",userList);
        page.setRows(userList.size());
        return "/site/admin/user";
    }

    @RequestMapping(path = "/data/admin",method = RequestMethod.GET)
    public String getAllAdmin(Model model, Page page){

        page.setPath("/data/admin");
        page.setLimit(5);
        List<User> userList = userService.findAdminOrModerator(page.getOffset(),page.getLimit(),1);
        model.addAttribute("userList",userList);
        page.setRows(userList.size());

        return "/site/admin/user";
    }
    @RequestMapping(path = "/data/moderator",method = RequestMethod.GET)
    public String getAllModerator(Model model, Page page){

        page.setPath("/data/moderator");
        page.setLimit(5);
        List<User> userList = userService.findAdminOrModerator(page.getOffset(),page.getLimit(),2);
        model.addAttribute("userList",userList);
        page.setRows(userList.size());

        return "/site/admin/user";
    }

    @RequestMapping(path="/data/allPost",method = RequestMethod.GET)
    public String getAllPost(Model model,Page page){
        page.setPath("/data/allPost");
        page.setLimit(5);
        List<DiscussPost> discussPosts = discussPostService.findDiscussPosts(0,page.getOffset(),page.getLimit(),0);
        page.setRows(discussPostService.findDiscussPostRows(0));
        model.addAttribute("discussPosts",discussPosts);
        return "/site/admin/post";
    }

    @RequestMapping(path="/data/searchPost",method = RequestMethod.GET)
    public String getAllPost(Model model,Page page,String keyword){

        int total = discussPostService.totalKeyword(keyword);
        page.setPath("/data/searchPost?keyword=" + keyword);
        page.setLimit(5);
        List<DiscussPost> discussPosts = discussPostService.findSomeDiscussPost(page.getOffset(),page.getLimit(),keyword);
        page.setRows(total);
        model.addAttribute("total",total);
        model.addAttribute("keyword",keyword);
        model.addAttribute("discussPosts",discussPosts);
        return "/site/admin/post";
    }

    @RequestMapping(path = "/data/allTopPost",method = RequestMethod.GET)
    public String getAllTopPost(Model model, Page page){

        int totalTop = discussPostService.totalTopPost(1);
        page.setPath("/data/allTopPost");
        page.setLimit(5);
        List<DiscussPost> discussPostList = discussPostService.findAllTopPost(page.getOffset(),page.getLimit(),1);
        model.addAttribute("discussPosts",discussPostList);
        page.setRows(totalTop);
        model.addAttribute("totalTop",totalTop);

        return "/site/admin/post";
    }

    @RequestMapping(path = "/data/allDeletePost",method = RequestMethod.GET)
    public String getAllDeletePost(Model model, Page page){

        int totalDel = discussPostService.totalDeleteOrWonderfulPost(2);
        page.setPath("/data/allDeletePost");
        page.setLimit(5);
        List<DiscussPost> discussPostList = discussPostService.findDeleteOrWonderfulPost(page.getOffset(),page.getLimit(),2);
        model.addAttribute("discussPosts",discussPostList);
        page.setRows(totalDel);
        model.addAttribute("totalDel",totalDel);

        return "/site/admin/post";
    }

    @RequestMapping(path = "/data/allWonderfulPost",method = RequestMethod.GET)
    public String getAllWonderfulPost(Model model, Page page){

        int totalWon = discussPostService.totalDeleteOrWonderfulPost(1);
        page.setPath("/data/allWonderfulPost");
        page.setLimit(5);
        List<DiscussPost> discussPostList = discussPostService.findDeleteOrWonderfulPost(page.getOffset(),page.getLimit(),1);
        model.addAttribute("discussPosts",discussPostList);
        page.setRows(totalWon);
        model.addAttribute("totalWon",totalWon);
        return "/site/admin/post";
    }

    @RequestMapping(path="/data/untop",method = RequestMethod.POST)
    @ResponseBody
    public String unTop(int id){
        if (discussPostService.findDiscussPostById(id).getType()==1){
            discussPostService.updateType(id,0);
            return CommunityUtil.getJSONString(0,"取消置顶成功");
        }else{
            discussPostService.updateType(id,1);
            return CommunityUtil.getJSONString(1,"置顶成功");
        }
    }

    @RequestMapping(path="/data/unWonderful",method = RequestMethod.POST)
    @ResponseBody
    public String unwonderful(int id){
        if (discussPostService.findDiscussPostById(id).getStatus()==1){
            discussPostService.updateStatus(id,0);
            return CommunityUtil.getJSONString(0,"取消加精成功");
        }else{
            discussPostService.updateStatus(id,1);
            return CommunityUtil.getJSONString(1,"加精成功");
        }
    }

    @RequestMapping(path="/data/unBlack",method = RequestMethod.POST)
    @ResponseBody
    public String unBlack(int id){
        if (discussPostService.findDiscussPostById(id).getStatus()==2){
            discussPostService.updateStatus(id,0);
            commentService.updateStatus(discussPostService.findDiscussPostById(id).getId(),0);
            return CommunityUtil.getJSONString(0,"取消拉黑成功");
        }else{
            discussPostService.updateStatus(id,2);
            commentService.updateStatus(discussPostService.findDiscussPostById(id).getId(),1);
            return CommunityUtil.getJSONString(1,"拉黑成功");
        }
    }
}
